<template>
  <div class>
    <div class="card tr mb2">
      <el-tooltip content="显示字段筛选" placement="bottom" effect="light">
        <el-popover placement="bottom" width="200" trigger="click">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选
          </el-checkbox>
          <el-checkbox-group v-model="showItemList" @change="handleCheckedChange">
            <el-checkbox v-for="(item,key) in allItemList" :key="key" :label="item">{{item.name}}</el-checkbox>
          </el-checkbox-group>
          <el-button slot="reference" icon="el-icon-menu" size="mini" type="primary" circle></el-button>
        </el-popover>
      </el-tooltip>
      <el-tooltip content="展开/收起表单" placement="bottom" effect="light">
        <el-button icon="el-icon-search" @click="formShow= !formShow" size="mini" type="primary" circle></el-button>
      </el-tooltip>
      <el-tooltip content="下载当前表格数据" placement="bottom" effect="light">
        <el-button icon="el-icon-download" @click="handleDownload" size="mini" type="primary" circle></el-button>
      </el-tooltip>
      <!-- <el-button size="mini" type="primary" icon="el-icon-plus" @click="showDialog">添加</el-button> -->
    </div>
    <div class="card" v-show="formShow">
      <el-form size="mini" :style="style" :model="ruleForm" ref="ruleForm" inline label-width="100px"
        class="demo-ruleForm">
                      <my-form-item label="流程定义编号" prop="id">
                  <el-input size="mini" v-model="ruleForm.id"></el-input>
                </my-form-item>
                      <my-form-item label="流程名称" prop="name">
                  <el-input size="mini" v-model="ruleForm.name"></el-input>
                </my-form-item>
                      <my-form-item label="关键字" prop="key">
                  <el-input size="mini" v-model="ruleForm.key"></el-input>
                      </my-form-item>
        <my-form-item>
          <el-button size="mini" type="primary" @click="submitForm('ruleForm')">查询</el-button>
          <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
        </my-form-item>
      </el-form>
    </div>
    <el-table stripe :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="60px" align="center" fixed="left" label="序号"></el-table-column>
      <el-table-column v-for="(item,index) in showItemList" align="center" :min-width="item.width || '' " :key="index" :label="item.name">
        <template slot-scope="scope">
          <template v-if="!item.formatter">{{ scope.row[item.key] }}</template>
          <template v-else>
            <span v-text="item.formatter(scope.row, item)"></span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150px" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showFlowNodes(scope.row)">节点配置</el-button>
          <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next"
        :total="total"></el-pagination>
    </el-card>
     
          <el-dialog title="节点列表" :visible.sync="dialogFlowNodes" :close-on-click-modal="false">
              <el-table  :data="NodesData" border style="width: 100%" size="mini" >
                <el-table-column type="index" width="60" align="center"  label="序号"></el-table-column>
                <el-table-column  prop="sid" min-width="120" align="center"  label="节点Key"></el-table-column>
                <el-table-column  prop="nodeName" min-width="120" align="center"  label="节点名称"></el-table-column>
                <el-table-column label="操作" min-width="120"  align="center">
                  <template slot-scope="scope">
                    <el-button size="mini" @click="showNodeForms(scope.row.id)">配置表单</el-button>
                    <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
                  </template>
                </el-table-column>
            </el-table>
        </el-dialog>

        <el-dialog title="节点表单信息列表" :visible.sync="dialogNodeForms" :close-on-click-modal="false">
              <el-table ref="multipleTable" :data="FormsData" border style="width: 100%" size="mini"  >
                <el-table-column align="center" width="30">
                  <template slot="header">
                    <el-button
                      class="el-icon-circle-plus"
                      style="padding:0"
                      type="text"
                      @click.prevent="addRow1()"
                    ></el-button>
                  </template>
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      style="padding:0"
                      class="el-icon-remove"
                      @click="handleDelete1(scope.$index, scope.row)"
                    ></el-button>
                  </template>
                </el-table-column>
                <el-table-column type="index" width="60" align="center"  label="序号"></el-table-column>
                <el-table-column  prop="name" min-width="120" align="center"  label="label名称"></el-table-column>
                <el-table-column  prop="value" min-width="120" align="center"  label="属性名称"></el-table-column>
                <el-table-column width="80" align="center"  label="是否启用">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.isTrue"  placeholder="请选择">
                      <el-option  value='1' label="是"></el-option>
                      <el-option  value='2' label="否"></el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="120"  align="center">
                  <template slot-scope="scope">
                    <el-button size="mini" @click="saveNodeForm(scope.row)">保存</el-button>
                    <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
                  </template>
                </el-table-column>
            </el-table>
        </el-dialog>
        <el-dialog width="70%" title="自定义表单信息" :visible.sync="addDialog">
      <el-form size="mini" inline :model="nodeform" :rules="rules" ref="person" label-width="80px">
        <my-form-item label="label名称" prop="name">
          <el-input
            v-model="nodeform.name"
            autocomplete="off"
            style="width: 200px;"
          ></el-input>
        </my-form-item>

        <my-form-item label="属性名称">
          <el-input
            v-model="nodeform.value"
            autocomplete="off"
            style="width: 200px;"
          ></el-input>
        </my-form-item>
        <my-form-item label="是否启用">
           <el-select v-model="nodeform.isTrue"  placeholder="请选择">
              <el-option  value='1' label="是"></el-option>
              <el-option  value='2' label="否"></el-option>
            </el-select>
        </my-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取消并返回</el-button>
        <el-button  type="primary" @click="handleAdd()">保存</el-button>
      </div>
    </el-dialog>
     
  </div>
  
</template>

<script>
  const defaultFormThead = [
    { key: "id", name: "流程定义编号",width:"120px" },
    { key: "name", name: "流程名称",width:"120px" },
    { key: "key", name: "关键字",width:"160px" },
    { key: "version", name: "版本号",width:"80px" },
    { key: "category", name: "应用服务名",width:"150px" },
    ];
  import EnumSelector from '@/pages/comm/enumSelector'
  export default {
    components: { EnumSelector },
    data() {
      return {
        addDialog:false,
        nodeform:{},
        multipleSelection:[],
        dialogNodeForms:false,
        FormsData:[],//节点表单信息
        NodesData:[],//工作流节点列表
        checkAll: true,
        isIndeterminate: false,
        allItemList: defaultFormThead,
        showItemList: defaultFormThead,
        formShow: false,
        dialogFormVisible: false,
        dialogFlowNodes:false,
        dialogEditVisible: false,
        offForm: true, //是否折叠
        folding: false, //表单是否超过一行
        formLabelWidth: "80px",
        tableData: [],
        total: 0,
        pageSize: 10,
        currentPage: 1,
        ruleForm: {
          serverName: '',
          bpmnName: '',
          bpmnPath: '',
          createUserName: '',
          createTime: '',
          deploymentId: '',
      },
    style: {},
    rules: {
      },
    };
  },

  mounted() {
    this.init();
    let formHeight = this.$refs.ruleForm.$el.clientHeight;
    if (formHeight > 62) {
      this.folding = true;
      this.style = {
        height: "62px",
        overflow: "hidden"
      };
    } else {
      this.folding = false;
      this.style = {
        height: "auto",
        overflow: "hidden"
      };
    }
  },
  computed: {
  },
  watch: {
    offForm: function(v) {
      if (v) {
        this.style = {
          height: "62px",
          overflow: "hidden"
        };
      } else {
        this.style = {
          height: "auto",
          overflow: "hidden"
        };
      }
    }
  },
  methods: {
    handleAdd(){
      if(this.nodeform.name==null||this.nodeform.name==''){
        this.$message({
                    message:"label名称不能为空",
                    type: "error"
                  });
                  return;
      }
      if(this.nodeform.value==null||this.nodeform.value==''){
        this.$message({
                    message:"属性名称不能为空",
                    type: "error"
                  });
                  return;
      }
      this.$api
          .formPost("/api/flowact/commTaskForm/doSave", this.nodeform)
          .then(res => {
            if (res.retHead.code == "0000000") {
              this.getNodeForms(this.nodeform.taskDefId);
              this.$message({
                  message: "保存成功！",
                  type: "success"
                });
                 this.nodeform.name='';
                  this.nodeform.value='';
                  this.nodeform.isTrue='';
            }else{
              this.$message({
                    message:res.retHead.message,
                    type: "error"
                  });
            }
          });
     
    },
    // 增加
    addRow1() {
      this.addDialog = true;
    },
    //删除
    handleDelete1(index, row) {
      this.$api
          .formPost("/api/flowact/commTaskForm/doRmById", {id:row.id})
          .then(res => {
            if (res.retHead.code == "0000000") {
              this.FormsData.splice(index, 1);
              this.$message({
                  message: "删除成功！",
                  type: "success"
                });
            }else{
              this.$message({
                    message:res.retHead.message,
                    type: "error"
                  });
            }
          });
      
     
    },
    cancle(){
      this.nodeform.name='';
      this.nodeform.value='';
      this.nodeform.isTrue='';
      this.addDialog=false;
    },
    saveNodeForm(row){
     this.$api
          .formPost("/api/flowact/commTaskForm/doModById", row)
          .then(res => {
            if (res.retHead.code == "0000000") {
              this.$message({
                  message: "保存成功！",
                  type: "success"
                });
            }else{
              this.$message({
                    message:res.retHead.message,
                    type: "error"
                  });
            }
          });
    },
    handleSelectionChange(val) {
        this.multipleSelection = val;
      },
    showNodeForms(id){
      this.nodeform.taskDefId=id;
      this.dialogNodeForms=true;
       this.getNodeForms(id);

    },
    getNodeForms(id){
      let params={
        taskDefId:id,
        pageNum:1,
        pageSize:20
      }
      this.$api
          .formPost("/api/flowact/commTaskForm/findListByTaskDefId", params)
          .then(res => {
            if (res.retHead.code == "0000000") {
              this.FormsData=res.retBody.list;
            }else{
              this.$message({
                    message:res.retHead.message,
                    type: "error"
                  });
            }
          });
    },
    showFlowNodes(row){
      this.dialogFlowNodes=true;
        let params={
      deploymentId:row.deploymentId,
      pageNum:1,
      pageSize:20
    }
    this.$api
        .formPost("/api/flowact/commTaskassignee/findListByDeploymentId", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
             this.NodesData=res.retBody.list;
             console.log(this.NodesData);
          }else{
            this.$message({
                  message:res.retHead.message,
                  type: "error"
                });
          }
        });
    },
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = [
          "流程名称",
          "流程部署id",
          "文件名称",
          "文件路径",
          "创建人id",
          "创建人",
          "部署时间",
          "流程更新人id",
          "部署id",
                    ];
        const filterVal = [
          "id",
          "bpmnName",
          "bpmnPath",
          "createUserId",
          "createUserName",
          "createTime",
          "serverName",
          "updateUserId",
          "deploymentId",
                    ];
        const data = this.formatJson(filterVal, this.tableData);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "table-list"
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
    handleCheckedChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === defaultFormThead.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < defaultFormThead.length;
    },
    handleCheckAllChange(val) {
      this.showItemList = val ? defaultFormThead : [];
      this.isIndeterminate = false;
    },
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    init() {
  
        let params = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        };
      this.$api
        .formPost("/api/flowact/deployments/getDefList", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
   
  submitForm(formName) {
    this.init();
  },
  resetForm(formName) {
    this.$refs[formName].resetFields();
  }
  },
  created() { }
};
</script>
<style lang='scss' scoped>
  .btns {
    text-align: right;
  }

  .icon-arrow {
    position: absolute;
    bottom: 5px;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #f1f1f1;

    i {
      background-color: #fff;
      position: absolute;
      color: #999;
      bottom: -7px;
      margin-left: 50%;
      transform: translateX(-50%);
      cursor: pointer;
    }
  }
</style>